// Copyright Epic Games, Inc. All Rights Reserved.

#include "LyraGameplayAbility_FromEquipment.h"
#include "LyraEquipmentInstance.h"
#include "Inventory/LyraInventoryItemInstance.h"

#if WITH_EDITOR
#include "Misc/DataValidation.h"
#endif

#include UE_INLINE_GENERATED_CPP_BY_NAME(LyraGameplayAbility_FromEquipment)

/**
 * 构造函数
 * @param ObjectInitializer - 对象初始化器
 */
ULyraGameplayAbility_FromEquipment::ULyraGameplayAbility_FromEquipment(const FObjectInitializer& ObjectInitializer)
	: Super(ObjectInitializer)
{
}

/**
 * 获取关联的装备
 * @return 关联的装备实例
 */
ULyraEquipmentInstance* ULyraGameplayAbility_FromEquipment::GetAssociatedEquipment() const
{
	if (FGameplayAbilitySpec* Spec = UGameplayAbility::GetCurrentAbilitySpec()) // 获取当前能力规格
	{
		return Cast<ULyraEquipmentInstance>(Spec->SourceObject.Get()); // 转换源对象为装备实例
	}

	return nullptr; // 未找到返回空
}

/**
 * 获取关联的物品
 * @return 关联的物品实例
 */
ULyraInventoryItemInstance* ULyraGameplayAbility_FromEquipment::GetAssociatedItem() const
{
	if (ULyraEquipmentInstance* Equipment = GetAssociatedEquipment()) // 获取关联的装备
	{
		return Cast<ULyraInventoryItemInstance>(Equipment->GetInstigator()); // 转换装备的发起者为物品实例
	}
	return nullptr; // 未找到返回空
}

#if WITH_EDITOR
/**
 * 数据验证
 * @param Context - 数据验证上下文
 * @return 数据验证结果
 */
EDataValidationResult ULyraGameplayAbility_FromEquipment::IsDataValid(FDataValidationContext& Context) const
{
	EDataValidationResult Result = Super::IsDataValid(Context); // 调用父类方法

PRAGMA_DISABLE_DEPRECATION_WARNINGS
	if (InstancingPolicy == EGameplayAbilityInstancingPolicy::NonInstanced) // 检查非实例化策略
PRAGMA_ENABLE_DEPRECATION_WARNINGS
	{
		Context.AddError(NSLOCTEXT("Lyra", "EquipmentAbilityMustBeInstanced", "Equipment ability must be instanced")); // 添加错误
		Result = EDataValidationResult::Invalid; // 设置为无效
	}

	return Result; // 返回结果
}

#endif